<?php
class Admin_Model_OghTinRaoMapper {

	protected $_dbTable;
	protected $db;

	public function setDbTable($dbTable) {
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}

	public function getDbTable() {
		if (null === $this->_dbTable) {
			$this->setDbTable('Admin_Model_DbTable_OghTinRao');
		}
		return $this->_dbTable;
	}

	public function getDb(){
		if ($this->db===null) {
			$this->db=Zend_Registry::get('connectDB');
		}
		return $this->db;
	}
	public function countItem($arrParam = null, $id = 0, $curruser = 0, $option = 'view', $query = null) {
		$paginator = $arrParam['paginator'];
		$ssFilter = $arrParam['ssFilter'];
		$keywords = '';

		$table = $this->getDbTable();
		$select = $table->select();
		$select->from($table, array("count(id) as totalItem"));
		//get current admin user
		if ($curruser>0) {
			$usradminmapper = new Admin_Model_UsersAdminMapper();
			$usrmodel = $usradminmapper->fetchAll('id='.$curruser,'view');
			$managecity = $usrmodel[0]->getThanh_pho_phu_trach();
			if($managecity>0){
				$select->where($table->getAdapter()->quoteInto('thanh_pho = ?', $managecity));
			}
		}
		
		if ($id>0) {
			$select->where($table->getAdapter()->quoteInto('id=?', $id));;
		}else {
			if (!empty($ssFilter['keywords'])) {
				$keywords = $ssFilter['keywords'];
				$select->where('id LIKE ? or tieu_de LIKE ? or mo_ta LIKE ?', '%'.$keywords.'%');
			}
			if (!empty($ssFilter['filter_col'])&&!empty($ssFilter['filter_val'])) {
				if (preg_match("/^[0-9]+$/", $ssFilter['filter_val']) && $ssFilter['filter_val'] > 0) $ssFilter['filter_val']--;
				$filterCol = trim($ssFilter['filter_col']);
				$filterVal = trim($ssFilter['filter_val']);
				$select->where($filterCol.' LIKE ?', '%'.$filterVal.'%');
			}
			$where = 'trang_thai != 4';
			if ($ssFilter['state'] == 1) {
				$where = 'trang_thai = 4';
			}
			$select->where($where);
		}
		
		if ($query != null) {
			$select->where($query);
		}
		
		$resultSet = $table->fetchAll($select);
		$array = $resultSet->toArray();
		return $array[0]['totalItem'];
	}

	public function fetch($arrParam = null, $id, $curruser = 0, $option = 'view', $query = null) {
		$paginator = $arrParam['paginator'];
		$ssFilter = $arrParam['ssFilter'];
		$keywords = '';
		$table = $this->getDbTable();
		$select = $table->select();
		$select->from($table);
		//get current admin user
		if ($curruser>0) {
			$usradminmapper = new Admin_Model_UsersAdminMapper();
			$usrmodel = $usradminmapper->fetchAll('id='.$curruser,'view');
			$managecity = $usrmodel[0]->getThanh_pho_phu_trach();
			if($managecity>0){
				$select->where($table->getAdapter()->quoteInto('thanh_pho = ?', $managecity));
			}
		}

		if ($id>0) {
			$select->where($table->getAdapter()->quoteInto('id=?', $id));;
		} else {
			if (!empty($ssFilter['keywords'])) {
				$keywords = $ssFilter['keywords'];
				$select->where('id LIKE ? or tieu_de LIKE ? or mo_ta LIKE ?', '%'.$keywords.'%');
			}
			if (!empty($ssFilter['col']) && !empty($ssFilter['order'])) {
				$col = $ssFilter['col'];
				$order = $ssFilter['order'];
				$select->order($col.' '.$order);
			}
			if (!empty($ssFilter['filter_col']) && !empty($ssFilter['filter_val'])) {
				if (preg_match("/^[0-9]+$/", $ssFilter['filter_val']) && $ssFilter['filter_val'] > 0) $ssFilter['filter_val']--;
				$filterCol = trim($ssFilter['filter_col']);
				$filterVal = trim($ssFilter['filter_val']);
				$select->where($filterCol.' = ?', $filterVal);
			}
			$where = 'trang_thai != 4';
			if ($ssFilter['state'] == 1) {
				$where = 'trang_thai = 4';
			}
			$select->where($where);
		}
		
		if ($query != null) {
			$select->where($query);
		}
		
		if ($paginator['itemCountPerPage'] > 0) {
			$page = $paginator['currentPage'];
			$rowCount = $paginator['itemCountPerPage'];
			$start = (int) $rowCount * ($page - 1);
			$select->limit($rowCount,$start);
		}

		return $this->processResultSet($table->fetchAll($select)->toArray(),$option);

	}
	/*
	 *fetchWithQuery
	 */
	public function fetchWithQuery($query=null,$option='view') {
		if ($query === NULL) {
			$resultSet = $this->getDbTable()->fetchAll();
		} else {
			$table = $this->getDbTable();
			$select = $table->select();
			$select->from($table)
			->where($query);
			$resultSet = $this->getDbTable()->fetchAll($select);
		}
		return $this->processResultSet($resultSet->toArray(),$option);
	}
	/*
	 *processResultSet
	 */
	private function processResultSet($resultSet,$option='view') {
		$entries = array();
		if($option=='view'){
			foreach ($resultSet as $row) {
				$entry = new Admin_Model_TableColumn_OghTinRao($row);
				$entries[] = $entry;
			}
			return $entries;
		}else{
			foreach ($resultSet as $row) {
				$entries[] = $row;
			}
			return $entries;
		}
	}
	/*
	 * save function
	 */
	public function save(Admin_Model_TableColumn_OghTinRao $item, $task = null) {
		$now=date("Y-m-d H:i:s",time());
		try {
			$id = $item->getId();
			if ($task=='changestatus') {
				$data = $item->toArray();
				$this->getDbTable()->update($data, $this->getDbTable()->getAdapter()->quoteInto('id=?', $id));
			}
			return TRUE;
		} catch (Exception $e) {
			return FALSE;
		}
	}
	
	/*
	 * change item
	 */
	public function change($id, $data = null) {
		$where = 'id IN (' . $id . ')';
		$this->getDbTable()->update($data, $where);
	}
	
	/*
	 * delete item
	 */
	public function delete($id) {
		$where = 'id IN (' . $id . ')';
		$this->getDbTable()->delete($where);
	}
}